package cn.gcjxsc.controller.api;

import cn.gcjxsc.consts.ErrorConst;
import cn.gcjxsc.data.vo.salesman.RecommendMemberVo;
import cn.gcjxsc.entity.member.Member;
import cn.gcjxsc.entity.salesman.RecommendMember;
import cn.gcjxsc.entity.salesman.Salesman;
import cn.gcjxsc.entity.salesman.SalesmanRecMemberStatics;
import cn.gcjxsc.exception.SQLException;
import cn.gcjxsc.service.integral.RecommendMemberService;
import cn.gcjxsc.service.integral.SalesmanService;
import cn.gcjxsc.service.member.MemberService;
import com.zgxl.common.util.AjaxResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author pumbaa
 * @since 1.0
 */
@RestController("apiRecommendController")
@RequestMapping("/api")
public class RecommendController {
    @Autowired
    private SalesmanService salesmanService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private RecommendMemberService recommendMemberService;

    /**
     * 查询会员是否已绑定业务员.
     * 三者合一:
     *  会员id,业务员对应的会员id,业务员对应的店铺id
     *
     * @param memberId 会员id
     * @return
     */
    @RequestMapping("/recommend/hasRelationship")
    public Boolean isMemberHasRelationship(@RequestParam("memberId") Integer memberId,
                                           @RequestParam("refMemberId") Integer refMemberId,
                                           @RequestParam("storeId") Integer storeId) {
        RecommendMember recommendMember = recommendMemberService.findByMemberIdAndSalesmanMemberIdAndStoreId(memberId, refMemberId, storeId);
        return recommendMember != null;
    }

    /**
     * 接受业务员邀请
     *
     * @return
     */
    @RequestMapping("/recommend/agreeInvitation")
    public AjaxResponse agreeInvitation(@RequestParam("memberId") Integer memberId, @RequestParam("salesmanId") Integer salesmanId) {
        Salesman salesman = salesmanService.findById(salesmanId);
        Member member = memberService.findById(memberId);
        if (null == salesman || null == member) {
            return AjaxResponse.ajaxFailure(ErrorConst.RECORD_NOT_EXIST);
        }
        if (recommendMemberService.isSalesmanRecommendMember(salesman, memberId)) {
            return AjaxResponse.ajaxFailure("该用户已存在推荐关系,不能重新绑定");
        }
        RecommendMember recommendMember = new RecommendMember();
        recommendMember.setSalesman(salesman);
        recommendMember.setMemberId(memberId);
        recommendMember.setCreated(System.currentTimeMillis() / 1000);
        recommendMember.setSalesmanMemberId(salesman.getMemberId());
        recommendMember.setStoreId(salesman.getStoreId());
        try {
            recommendMemberService.addRecommendMember(recommendMember);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SQLException();
        }
        return AjaxResponse.ajaxSuccess();
    }

    /**
     * 查询指定业务员的推荐会员信息列表
     *
     * @param refMemberId 业务员会员id
     * @param page        当前页
     * @param pageSize    每页记录数
     * @return
     */
    @RequestMapping("/recommend/myMemberList")
    public List<RecommendMemberVo> queryRelationshipList(@RequestParam("memberId") Integer refMemberId,
                                                         @RequestParam(value = "page", defaultValue = "1", required = false) int page,
                                                         @RequestParam(value = "pageSize", defaultValue = "30", required = false) int pageSize) {
        page = Math.abs(page);
        List<RecommendMember> recommendMemberList = recommendMemberService.findByRefMemberId(refMemberId, page, pageSize);
        List<Integer> ids = new ArrayList<>();
        List<RecommendMemberVo> voList = new ArrayList<>();
        if (null != recommendMemberList && recommendMemberList.size() > 0) {
            Map<Integer, RecommendMemberVo> voMap = new HashMap<>();
            for (RecommendMember recommendMember : recommendMemberList) {
                ids.add(recommendMember.getMemberId());

                RecommendMemberVo vo = new RecommendMemberVo();
                vo.setId(recommendMember.getId());
                vo.setMemberId(recommendMember.getMemberId());
                vo.setRefMemberId(recommendMember.getSalesmanMemberId());
                vo.setSalesmanId(recommendMember.getSalesman().getId());
                voList.add(vo);

                voMap.put(recommendMember.getMemberId(), vo);
            }

            List<SalesmanRecMemberStatics> refMemberStatics = recommendMemberService.findRefMemberStatics(ids, refMemberId);
            for (SalesmanRecMemberStatics refMemberStatic : refMemberStatics) {
                Integer memberId = refMemberStatic.getMemberId();
                RecommendMemberVo vo = voMap.get(memberId);
                if (null != vo) {
                    vo.setAmount(refMemberStatic.getAmount());
                }
            }
        }

        return voList;
    }


}
